package com.zxc.reggie.service.lmpl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.reggie.common.BaseContext;
import com.zxc.reggie.common.CustomException;
import com.zxc.reggie.common.R;
import com.zxc.reggie.entity.*;
import com.zxc.reggie.mapper.OrdersMapper;
import com.zxc.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {


    @Resource
    private ShoppingCartService shoppingCartService;

    @Resource
    private UserService userService;

    @Resource
    private AddressBookService addressBookService;

    @Resource
    private OrderDetailService orderDetailService;

    @Override
    public R<String> submit(Orders orders) {

        Long userId = BaseContext.getCurrentId();
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        List<ShoppingCart> shoppingCarts = shoppingCartService.query().eq("user_id", userId).list();
        long id = IdWorker.getId();
        AtomicInteger atomicInteger = new AtomicInteger(0);



        User user = userService.query().eq("id", userId).one();
        AddressBook addressBook = addressBookService.query().eq("user_id", userId).eq("is_default", 1).one();

        if (shoppingCarts.size() == 0) {
            throw new CustomException("购物车为空，不能下单");
        }

        List<OrderDetail> details = shoppingCarts.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();

            orderDetail.setOrderId(id);
            BeanUtils.copyProperties(shoppingCart, orderDetail,"id");
            atomicInteger.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setOrderTime(LocalDateTime.now());
        orders.setUserId(userId);
        orders.setId(id);
        orders.setNumber(String.valueOf(id));
        orders.setUserName(user.getName());
        orders.setAddressBookId(addressBook.getId());
        orders.setPhone(addressBook.getPhone());
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        orders.setConsignee(addressBook.getConsignee());
        orders.setStatus(1);
        orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDetail() == null ? "" : addressBook.getDetail())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
        );
        orderDetailService.saveBatch(details);
        save(orders);

        return R.success("下单成功");
    }
}
